package cn.iocoder.yudao.module.project.service.internalcodeorderedit;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.internalcodeorderedit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.internalcodeorderedit.InternalCodeOrderEditDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.internalcodeorderedit.InternalCodeOrderEditMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 订单编辑-删除相关内码 Service 实现类
 *
 * @author wangzuoyang
 */
@Service
@Validated
public class InternalCodeOrderEditServiceImpl implements InternalCodeOrderEditService {

    @Resource
    private InternalCodeOrderEditMapper internalCodeOrderEditMapper;

    @Override
    public Long createInternalCodeOrderEdit(InternalCodeOrderEditSaveReqVO createReqVO) {
        // 插入
        InternalCodeOrderEditDO internalCodeOrderEdit = BeanUtils.toBean(createReqVO, InternalCodeOrderEditDO.class);
        internalCodeOrderEditMapper.insert(internalCodeOrderEdit);
        // 返回
        return internalCodeOrderEdit.getRecycleId();
    }

    @Override
    public void updateInternalCodeOrderEdit(InternalCodeOrderEditSaveReqVO updateReqVO) {
        // 校验存在
        validateInternalCodeOrderEditExists(updateReqVO.getRecycleId());
        // 更新
        InternalCodeOrderEditDO updateObj = BeanUtils.toBean(updateReqVO, InternalCodeOrderEditDO.class);
        internalCodeOrderEditMapper.updateById(updateObj);
    }

    @Override
    public void deleteInternalCodeOrderEdit(Long id) {
        // 校验存在
        validateInternalCodeOrderEditExists(id);
        // 删除
        internalCodeOrderEditMapper.deleteById(id);
    }

    private void validateInternalCodeOrderEditExists(Long id) {
        if (internalCodeOrderEditMapper.selectById(id) == null) {
            throw exception(INTERNAL_CODE_ORDER_EDIT_NOT_EXISTS);
        }
    }

    @Override
    public InternalCodeOrderEditDO getInternalCodeOrderEdit(Long id) {
        return internalCodeOrderEditMapper.selectById(id);
    }

    @Override
    public PageResult<InternalCodeOrderEditDO> getInternalCodeOrderEditPage(InternalCodeOrderEditPageReqVO pageReqVO) {
        return internalCodeOrderEditMapper.selectPage(pageReqVO);
    }

}